
import React from 'react';

import styles from './index.css';
import GoodsNav from "../../../components/admin/GoodsNav";
import AdminModelPros from "../../../components/pros/AdminModelPros";
import GoodsList from "../../../components/admin/GoodsList";
import GoodsAdd from "../../../components/admin/GoodsAdd1";
import LoginService from "../../../business/login/LoginService";
import {message} from "antd";
import AdminList from "../../../components/admin/AdminList";
import RuleList from "../../../components/admin/RuleList";
import RuleAdd from "../../../components/admin/RuleAdd";
import RuleListAndOption from "../../../components/admin/RuleListAndOption";
import GoodsOrderList from "../../../components/admin/GoodsOrderList";
import PointList from "../../../components/admin/PointList";
import PointAdd from "../../../components/admin/PointAdd";
import EditableTable from "../../../components/admin/GoodsList1";
import CountPoint from "../../../components/admin/CountPoint";
import ClearPoints from "../../../components/admin/ClearPoints";



class Index extends React.Component<AdminModelPros,any>{

  constructor(props) {
    super(props);
    let adminUsername = sessionStorage.getItem("adminUsername");
    let adminRole = sessionStorage.getItem(adminUsername + "_role");

    this.state={adminRole:adminRole};
    this.loginService = new LoginService();
    this.setAdminRole = this.setAdminRole.bind(this);

  }
  loginService :LoginService;
  componentDidMount(){
    let adminUsername = sessionStorage.getItem("adminUsername");
    if(adminUsername ==undefined || adminUsername ==''){
      window.document.location.href='/point-shopping/admin/login';
    }else{
      let adminRole = sessionStorage.getItem(adminUsername + "_role");
      if(adminRole == undefined ||adminRole == ''){
        let data ={
          token:sessionStorage.getItem("adminLoginToken")
        };
        this.loginService.adminQueryRole(data,data.token,this.setAdminRole);
      }else{
        this.setState({adminRole:adminRole});
      }
    }
  }

  setAdminRole(data,isSuccess){
    if(!isSuccess || data.code != "200"){
      message.info(data.message);
    }else{
      let adminUsername = sessionStorage.getItem("adminUsername");
      sessionStorage.setItem(adminUsername+"_role",data.data.role);
      sessionStorage.setItem(adminUsername+"_host",data.data.host);
      this.setState({adminRole:data.data.role});
    }
  }

  showBody(){
    const {state} = this.props.location;
    const {adminRole} = this.state;
    if(adminRole =='ROLE_ROOT'){
      if(state== undefined){
        return (<AdminList/>);
      }else if(state.bodyName== 'ruleListAndOption'){
        return (<RuleListAndOption/>);
      }else if(state.bodyName== 'clearPoints'){
        return (<ClearPoints/>);
      }else{
        return (<AdminList/>);
      }
    }else if(adminRole =='ROLE_RP_ADMIN'){
      if(state== undefined) {
        return (<RuleList/>);
      }else if(state.bodyName =='addRule'){
        return (<RuleAdd/>);
      }else if(state.bodyName =='ruleList'){
        return (<RuleList/>);
      }else if(state.bodyName =='index'){
        return (<EditableTable/>);
      }else if(state.bodyName =='addGift'){
        return (<GoodsAdd/>);
      }else if(state.bodyName =='giftOrder'){
        return (<GoodsOrderList/>);
      }else if(state.bodyName =='pointList'){
        return (<PointList/>);
      }else if(state.bodyName =='addPoint'){
        return (<PointAdd/>);
      }else if(state.bodyName =='countPoint'){
        return (<CountPoint/>);
      }else{
        return (<RuleList/>);
      }
    }else if(adminRole =='ROLE_CP_ADMIN'){
      if(state== undefined){
        return (<GoodsList/>);
      }else if(state.bodyName =='addGift'){
        return (<GoodsAdd/>);
      }else if(state.bodyName =='giftOrder'){
        return (<GoodsOrderList/>);
      }else{
        return (<GoodsList/>);
      }
    }else if(adminRole =='ROLE_RP_SYSTEM'){
      return (<RuleList/>);
    }else{
      return '';
    }
  }

  showTitle(){
    const {adminRole} = this.state;
    const {state} = this.props.location;
    if(adminRole =='ROLE_ROOT') {
      if(state!= undefined&&state.bodyName == 'ruleListAndOption'){
        return "管理员审核活动";
      }else{
        return "管理员管理";
      }

    }else if(adminRole =='ROLE_RP_ADMIN'){
      if(state== undefined) {
        return "积分规则管理";
      }else if(state.bodyName=="giftOrder" ||state.bodyName=="addGift" ||state.bodyName=="index" ){
        return "礼品管理";
      }else{
        return "积分规则管理";
      }
    }else{
      return "礼品管理";
    }

  }

  render() {
    const {state} = this.props.location;
    return (
      <div className={styles.normal}>
        <div>
          <div className={[styles.section,styles["section-subnav"]].join(' ')}>
            <div className={styles.section_subnav}>
              <p className={styles.subnav_title}>{this.showTitle()}</p>
            </div>
          </div>

          <div className={styles.section}>
            <div className="section_user">
              <GoodsNav bodyName={state==undefined?"":state.bodyName} role={this.state.adminRole}/>
              {this.showBody()}
            </div>
          </div>

        </div>
      </div>
    );
  }
}

export default Index;
